#include <iostream>
#include <stdexcept>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "sdlevent.h"
#include "sdlbutton.h"
#include "sdltext.h"
#include "sdlmenu.h"
#include "game.h"
#include "CLI.h"


using namespace std;

SDLMenu::SDLMenu(struct SDL_Surface *target_surface) throw(sdlmenu_error)
{
       screen_ = target_surface;
       background_ = NULL;
       button_[0] = NULL;
       button_[1] = NULL;
       button_[2] = NULL;

       // Create text for the buttons
       try
       {
	      button_text_[0] = new SDLText(255,255,255,20,"CHECKBK0.TTF", "New game");
	      button_text_[1] = new SDLText(255,255,255,20,"CHECKBK0.TTF", "Edit");
	      button_text_[2] = new SDLText(255,255,255,20,"CHECKBK0.TTF", "Exit");
       }
       catch(sdltext_error &texterror)
       {
	      cout << texterror.what();
	      throw sdlmenu_error("SDLMenu: Could not create text for buttons! Cannot continue!");
	      abort();
       }


       // Load images for the main menu buttons
       try{
	      button_[0] = new SDLButton("unew_game.png", "cnew_game.png", 462, 250, screen_, button_text_[0]);
	      button_[1] = new SDLButton("unew_game.png", "cnew_game.png", 462, 300, screen_, button_text_[1]);
	      button_[2] = new SDLButton("unew_game.png", "cnew_game.png", 462, 350, screen_, button_text_[2]);
       }
       catch(sdlbutton_error &error)
       {
	      cout << error.what();
	      throw sdlmenu_error("SDLMenu: Could not load buttons! Cannot continue!");
	      abort();
       }

       // Load background image
       background_ = IMG_Load("adventure2/graphics/backdrops/mbackground.png");
       if(background_ == NULL)
       {
	      throw sdlmenu_error("SDLMenu: Could not load background image!");
       }

       ba_dst_.h = background_->h;
       ba_dst_.w = background_->w;
       ba_dst_.x = 0;
       ba_dst_.y = 0;

       SDL_BlitSurface(background_, NULL, screen_, &ba_dst_);
       for(int i = 0;i<3;i++)
       {
	      button_[i]->render();
       }
}

SDLMenu::~SDLMenu()
{
       if(background_ != NULL)
       {
	      SDL_FreeSurface(background_);
       }
       
       if(button_[0] != NULL)
       {
	      delete button_[0];
       }

       if(button_[1] != NULL)
       {
	      delete button_[1];
       }

       if(button_[2] != NULL)
       {
	      delete button_[2];
       }
}

void SDLMenu::render()
{
       SDL_BlitSurface(background_, NULL, screen_, &ba_dst_);
       for(int i = 0;i<3;i++)
       {
	      button_[i]->render();
       }

       SDL_Flip(screen_);
}
void SDLMenu::run()
{
       running_ = true;

       while(running_)
       {
	      // Read keyboard
	      event_.Poll();

	      if(event_.mouse_clicked() == true)
	      {		     
		     if(button_[0]->is_clicked(event_.GetMouseX(), event_.GetMouseY()) == true)
		     {
			    render();
			    Game game_(screen_);
			    game_.run();
		     }		     
		     if(button_[1]->is_clicked(event_.GetMouseX(), event_.GetMouseY()) == true)
		     {
			    render();
			    CLI interface;
			    interface.run();
			    running_ =  false;
		     }
		     if(button_[2]->is_clicked(event_.GetMouseX(), event_.GetMouseY()) == true)
		     {
			    render();
			    running_ =  false;
		     }
	      }	     	      

	      render();
	      
       }
}
